// @flow

export const SAVE_ID = 'SAVE_ID'
export const SAVE_DEVICE_DATA = 'SAVE_DEVICE_DATA'

type Action = {
  type: string,
  id: string,
  data: Object
}

export const saveGroupId = (id: string) => {
  return {
    type: SAVE_ID,
    id
  }
}

export const saveDeviceData = (data: Object) => {
  return {
    type: SAVE_DEVICE_DATA,
    data
  }
}

const ACTION_HANDLERS = {
  [SAVE_ID]: (state, { id }: Action) => Object.assign({}, state, {
    groupId: id
  }),
  [SAVE_DEVICE_DATA]: (state, { data }: Action) => Object.assign({}, state, {
    deviceData: data
  })
}

type State = Object

const initialState = {
  groupId: '',
  deviceData: {}
}

export default function groupIdReducer (state: State = initialState, action: Action) {
  const handler = ACTION_HANDLERS[action.type]

  return handler ? handler(state, action) : state
}
